package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.config.YmlConfig;
import com.qiche.enums.ResultEnum;
import com.qiche.model.CategoryBanner;
import com.qiche.model.User;
import com.qiche.service.CategoryBannerService;
import com.qiche.util.FastDFSUtils;
import com.qiche.util.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * User: ZHS
 * Date: 2018/7/12 11:09
 */
@RestController
@RequestMapping("/banners")
public class CategoryBannerController {
    @Autowired
    private CategoryBannerService categoryBannerService;
    @Autowired
    private YmlConfig ymlConfig;

    @ApiOperation(value = "查询广告轮播图")
    @GetMapping
    public Map<String, Object> getAll(
                                      @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "10") int limit){
        Map<String,Object> map = new HashMap<String,Object>();
        PageInfo<CategoryBanner> pageInfo =categoryBannerService.selectAllByPage(page,limit);
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }
    @ApiOperation(value = "上传广告图片")
    @PostMapping("/upload")
    public Result uploadLogo(MultipartFile file) {
        try {
            User currentUser = UserUtils.getCurrentUser();
            if (currentUser.getMerchantId() == null) {
                return Result.createByErrorMessage("登录用户失效!");
            }
            String paths = FastDFSUtils.uploadPic(file);
//            String paths = "http://img1.imgtn.bdimg.com/it/u=1193474556,1223904415&fm=27&gp=0.jpg";
            return Result.createByResultEnum(ResultEnum.SUCCESS, ymlConfig.getFastdfsDomainUrl() + paths);
//            return Result.createByResultEnum(ResultEnum.SUCCESS, paths);
        } catch (MultipartException e){
            e.printStackTrace();
            return Result.createByErrorMessage("上传失败,图片超过大小限制");
        }catch (Exception e) {
            e.printStackTrace();
            return Result.createByResultEnum(ResultEnum.FAILURE);
        }
    }
    @ApiOperation(value ="添加banner图" )
    @PostMapping
    public Result add(@RequestBody CategoryBanner category) {
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser.getMerchantId() == null) {
            return Result.createByErrorMessage("登录用户失效!");
        }
        category.setCreatedDate(new Date());
        category.setUpdateDate(new Date());
        boolean isOk = categoryBannerService.saveBanner(category)>0?true:false;
        if(isOk){
            return Result.createBySuccessMessage("添加成功");
        }
        return Result.createByErrorMessage("添加失败");
    }
    @ApiOperation(value = "根据ID获取banner信息")
    @GetMapping(value = "/showBanner/{id}")
    public Result<CategoryBanner> getById(@PathVariable("id") Long id) {
        CategoryBanner card = new CategoryBanner();
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser.getMerchantId() == null) {
            return Result.createByErrorMessage("登录用户失效!");
        }
        CategoryBanner categoryBanner = categoryBannerService.selectByKey(id);
        if (categoryBanner!=null){
            return  Result.createBySuccess(categoryBanner);
        }
        return Result.createByErrorMessage("查询失败");
    }
    @ApiOperation(value ="修改banner图" )
    @PostMapping("/edit")
    public Result edit(@RequestBody CategoryBanner category) {
        category.setUpdateDate(new Date());
        boolean isOk = categoryBannerService.updateNotNull(category)>0?true:false;
        if(isOk){
            return Result.createBySuccessMessage("添加成功");
        }
        return Result.createByErrorMessage("添加失败");
    }

    @ApiOperation(value = "根据ID删除banner图")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser.getMerchantId() == null) {
            return Result.createByErrorMessage("登录用户失效!");
        }
        int count = categoryBannerService.delete(id);
        if (count>0){
            return Result.createBySuccessMessage("删除成功");
        }else{
            return Result.createByErrorMessage("删除失败");
        }
    }
    @ApiOperation(value = "根据ID移动banner图")
    @PostMapping(value = "/move")
    public Result move(Long id, String type) {
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser.getMerchantId() == null) {
            return Result.createByErrorMessage("登录用户失效!");
        }
       return  categoryBannerService.upOrDown(id,type);

    }

}
